package viv.karaoke.book;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class userDataSource {
	// Database fields
	private SQLiteDatabase database;
	private mySQLiteHelper dbHelper;
	
	private String[] userColumns = {
				 mySQLiteHelper.USER_ID,
				 mySQLiteHelper.USER_NAME
				 };

	public userDataSource(Context context) {
	dbHelper = new mySQLiteHelper(context);
	}
	
	public void open() throws SQLException {
	database = dbHelper.getWritableDatabase();
	}
	
	public void close() {
	dbHelper.close();
	}

	public user createUser(String userName) {
		ContentValues values = new ContentValues();
		values.put(mySQLiteHelper.SONG_NAME, userName);
			    
		long insertId = database.insert(mySQLiteHelper.TABLE_USER, null,values);
			    
		Cursor cursor = database.query(mySQLiteHelper.TABLE_USER, userColumns, mySQLiteHelper.USER_ID + " = " + insertId, null, null, null, null);
	
		user newUser = new user();
		try{
			cursor.moveToFirst();
			newUser = cursorToUser(cursor);
		}
		catch(Exception ex)
		{
		}
		finally
		{
			//Make sure to close the cursor
			cursor.close();
		}
		
	    return newUser;
	}
	
	public void updateUser(user aUser) {
		    long id = aUser.getUserId();
		    ContentValues values = new ContentValues();
		    values.put(mySQLiteHelper.USER_NAME, aUser.getUserName());
		    database.update(mySQLiteHelper.TABLE_USER, values, mySQLiteHelper.USER_ID+ " = " + id, null);
	}

	public boolean isUserExists(String userName) {		  	    
		  Cursor cursor = database.query(mySQLiteHelper.TABLE_USER, 
						userColumns, mySQLiteHelper.USER_NAME + " = " + userName, null, null, null, null, null);
		    
			try{

				if(cursor != null)
				{
					
					return true;
			    }
			}
			catch(Exception ex)
			{
			}
		    finally
		    {
		    	//Make sure to close the cursor
		    	cursor.close();
		    }
		    return false;
		  }

	public List<user> getAllUsers() {
		  List<user> users = new ArrayList<user>();
		  	    
		  Cursor cursor = database.query(mySQLiteHelper.TABLE_USER, 
						userColumns, null, null, null, null, null);
		    
			try{
			    cursor.moveToFirst();
			    while (!cursor.isAfterLast()) {
			      user user = cursorToUser(cursor);
			      users.add(user);
			      cursor.moveToNext();
			    }
			}
			catch(Exception ex)
			{
			}
		    finally
		    {
		    	//Make sure to close the cursor
		    	cursor.close();
		    }
		    return users;
		  }
	
	public user getFromUserId(long userId) {
	    user user = new user();
	    
		Cursor cursor = database.query(mySQLiteHelper.TABLE_USER, 
					userColumns, mySQLiteHelper.USER_ID + " = " + userId, null, null, null, null, null);
	    
		if(cursor != null)
		try{
		    cursor.moveToFirst();
		    user.setUserId(cursor.getLong(0));
		    user.setUserName(cursor.getString(1));
		    
		    return user;
		}
		catch(Exception ex)
		{
		}
	    finally
	    {
	    	//Make sure to close the cursor
	    	cursor.close();
	    }  
	    
	    
	    return null;
	  }

	private user cursorToUser(Cursor cursor) {
	    user user = new user();
	    user.setUserId(cursor.getLong(0));
	    user.setUserName(cursor.getString(1));
	    return user;
	  }	
}
